package com.action.basic;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.action.common.Ajax;
import com.action.common.WEB;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.basic.ACCN;
import com.service.basic.IACCNService;
import com.util.ActionLog;

public class ACCNAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;
	
	private IACCNService accnService;
	private String  accn_no_up = "";
	private String  accn_no_ups = "";
	private Map<String,String> maps;
	private List<ACCN> accns;

	//setter getter
	
	public void setAccnService(IACCNService accnService) {
		this.accnService = accnService;
	}
	
	public Map<String, String> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, String> maps) {
		this.maps = maps;
	}

	public String getAccn_no_ups() {
		return accn_no_ups;
	}

	public void setAccn_no_ups(String accn_no_ups) {
		this.accn_no_ups = accn_no_ups;
	}

	public List<ACCN> getAccns() {
		return accns;
	}

	public void setAccns(List<ACCN> accns) {
		this.accns = accns;
	}

	public String getAccn_no_up() {
		return accn_no_up;
	}

	public void setAccn_no_up(String accn_no_up) {
		this.accn_no_up = accn_no_up;
	}

	//action method
	
	public String initACCNData(){
		ActionLog.info(this.getClass(),"["+WEB.getClassName(this.getClass())+"]:【初始化會計科目】");
		this.setAccns(accnService.findAllACCNByAccNoUp(this.getAccn_no_up()));
		String [] temps = this.getAccn_no_ups().split(":");
		Map<String,String>  map = new HashMap<String,String>();
		for(int i = 0 ; i < temps.length ; i++){
			List<ACCN> list = accnService.findAllACCNByAccNoUp(temps[i]);
			map.put(temps[i], this.spanListMenu(list));
		}
		this.setMaps(map);
		return SUCCESS;
	}
	
	public void findACCNDataByAjax(){
		ActionLog.info(this.getClass(),"["+WEB.getClassName(this.getClass())+"]:【查询會計科目子节点】");
		List<ACCN> accns = accnService.findAllACCNByAccNoUp(this.getAccn_no_up());
		String menus = this.spanListMenu(accns);
		try {
			Ajax.sendMsgAjax(menus);
		} catch (IOException e) {
			System.out.println("com.action.basic.ACCNAction.findACCNDataByAjax異常");
		}
	}
	
	public void findACCNTreeDataByAjax(){
		ActionLog.info(this.getClass(),"["+WEB.getClassName(this.getClass())+"]:【查询會計科目子节点】");
		List<ACCN> accns = accnService.findAllACCNByAccNoUp(this.getAccn_no_up());
		String menus = this.spanListMenu(accns);
		Ajax.sendMsg(menus);
	}
	
	public String  spanListMenu(List<ACCN> accns){
		StringBuffer sb = new StringBuffer("[");
		int count = 0;
		for(ACCN accn : accns){
			String flag = accn.getCLS().equals("0")?"true":"false";
			String href = accn.getCLS().equals("0")?"":"";
			String cls =  accn.getCLS().equals("0")?",'iconCls':'leaf'":"";
			sb.append("{'id':" + "'" + accn.getACC_NO() + "'"
					+ ",'text':" + "'" + accn.getACC_NO() +"&nbsp;&nbsp;&nbsp;&nbsp;"+accn.getNAME() + "'"
					+ ",'leaf':" + ""+flag+","
					+ "'href':'" + href + "'"+cls+"");
			sb.append("}");
			if (count < accns.size() - 1) {
				sb.append(",");
			}
			count++;
		}
		sb.append("]");
		return sb.toString();
	}

}
